<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Settings</title>

<link href="style.css" rel="stylesheet" type="text/css" />

<SCRIPT TYPE="text/javascript">
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=500,height=400,scrollbars=yes');
return false;
}
</SCRIPT>

</head>
<body>
<div id="container">
<?php include("menu.php");?>

<?php

//function to get xml data,  the function is called with a name of a domain or po

function getSettings($object)

{
	$configfile='/etc/opt/gwack/gwack.xml';
	$xml = simplexml_load_file($configfile);

	//loop thru the servers, then load xml data and an array for the matching server name

	foreach ($xml->server as $s)
	{
		if ($s->name == $object)
		{
			$gwname = $s->name;
			$ipaddr = $s->ipaddr;
			$remotepass = $s->remotepass;
			$remotepath = $s->remotepath;
			$remoteuser = $s->remoteuser;
			$mounttype = $s->mounttype;
		

			$objectsettings = array(
				"gwname" => $gwname, 
				"ipaddr" => $ipaddr,
				"remotepass" => $remotepass,
				"remotepath" => $remotepath,
				"remoteuser" => $remoteuser,
				"mounttype" => $mounttype
			);

			// return the array to the caller
				
			return $objectsettings;
		}
	}
}

//Get the dom or po name from the url link

$name = $_GET[name];

//call teh function to load the xml data

$settings = getSettings($name);


// function to remove server if "delete" was selected

if (isset($_POST['delserver_x']))
{
	$object = $_POST['gname'];
	
	//load teh xml file again

    $configfile='/etc/opt/gwack/gwack.xml';
  	$x = simplexml_load_file($configfile);
	
	//loop thru to find the right dom/po
    foreach ($x->server as $s)
   	{
        if ($s->name == $object)
        {

		// this part sux,  simplexml won't let me delete a node
		//  so we have to load the node into a dom object
		//  and delete it that way.
        $dom=dom_import_simplexml($s);
        $dom->parentNode->removeChild($dom);
		}
	}
		//write the file back to disk and refresh
        file_put_contents($configfile, $x->asXML());
		header("Location:index.php");
}


// function to change data if "Saved was selected
// See comments for similar function in globalsettings.php
// for details 

if (isset($_POST['save_x']))
{

	$configfile='/etc/opt/gwack/gwack.xml';
	$xml = simplexml_load_file($configfile);

	$gwname = $_POST['gwname'];
	$ipaddr = $_POST['ipaddr'];
	$remotepath = $_POST['remotepath'];
	$remoteuser = $_POST['remoteuser'];
	$remotepass = $_POST['remotepass'];
	$mounttype = $_POST['mounttype'];

	foreach ($xml->server as $s)
	{
		if ($s->name == $gwname)
		{
			$s->ipaddr = $ipaddr;
			$s->remotepath = $remotepath;
			$s->remoteuser = $remoteuser;
			$s->remotepass = $remotepass;
			$s->mounttype = $mounttype;

			file_put_contents($configfile, $xml->asXML()) or die("save failed");
		}
	}			
			
	$settings = getSettings($gwname);
}

?>

<!-- create the form -->



<div id="content">
	<div id="leftBox">
		<div class="contentBox">
            <div class="contentBoxTop">
              <h3><?php echo $name;?> Settings</h3>
            </div>
            <div class="innerContent">

			<table class="sTable">	
			<tr>
			<form id='form1' name='form1' method='post' action='<?php echo $_SERVER['PHP_SELF'];?>'/>
			<input name='gwname' type='hidden' value="<?php echo $settings[gwname];?>" />
			<td class="firstCol"><b>Server IP: </b></td>
			<td class="secondCol"><input name='ipaddr' type='text' value="<?php echo $settings[ipaddr];?>" /></td>
			</tr>
			<tr>
			<td><b>Remote Path:</b></td>
			<td><input name='remotepath' type='text' value="<?php echo $settings[remotepath];?>" /></td>
			</tr>
			<tr>
			<td><b>Remote User:</b></td>
			<td><input name='remoteuser' type='text' value="<?php echo $settings[remoteuser];?>" /></td></tr>
			<tr>
			<td><b>Remote Password:</b></td>
			<td><input name='remotepass' type='password' value="<?php echo $settings[remotepass]; ?>" /></td>
			</tr>
			<tr>
			<td><b>Mount Type:</b></td>
			<td>Samba<input type='radio' value='Samba' name='mounttype'
			<?php if ($settings[mounttype] == "samba" or $settings[mounttype] == "Samba") echo 'checked';?> >
    			NCP<input type='radio' value='NCP' name='mounttype'
			<?php if ($settings[mounttype] == "NCP") echo 'checked';?> ></td>
			</tr>
	
			</table>
			<p>
			<table>
			<tr>
			<td><input name='save' type='image' src='images/save.jpg' /></td>
			<td><input name='delserver' type='image' src='images/delete.jpg'></td>
			<input name='gname' value='<?php echo $settings[gwname];?>' type='hidden'/>
			</form>
			<td><input name='help' type='image' src='images/help.jpg' onClick="return popup('help.html', 'serverhelp')" ></td></tr>
			</table>

	
			
			</div><!-- innercontent -->

		</div><!-- Contentbox -->
	</div><!-- left box -->



<div id="sidebar" class="rightBoxes">
  <div class="rightBoxesTop"><h3>Dom - PO Settings</h3></div>
  <div class="rightContent">
	<ul id="sidebarLinks">
	<li><a href='settings.php'>Global Settings</a>
	<?php
	// Create an array of domain and po names
    $configfile = '/etc/opt/gwack/gwack.xml';
    $obj_xml = simplexml_load_file($configfile);
    foreach ( $obj_xml->server as $server )
    {
        if ($server->type == "po") {
            $pos[] = $server->name;
        }
        if ($server->type == "domain"){
            $doms[] = $server->name;
        }
    }
	foreach ($doms as $dom) {
       		echo "<li><a href='objectsettings.php?name=".$dom."' target='_self' title='".$dom."'>".$dom." Domain Settings</a></li>";
        }
        foreach ($pos as $po) {
        	echo "<li><a href='objectsettings.php?name=".$po."' target='_self' title='".$po."'>".$po." Post Office Settings</a></li>";
        }?>

	</ul>
  	</div>
	</div><!-- end of #sidebar -->
</div><!-- content -->
</div><!-- container -->


<?php include('footer.html'); ?>


</body></html>



